import Vue from 'vue'
import VueRouter from 'vue-router'
import Logon from '../components/logon.vue'
import List from '../components/list.vue'
import User from '@/components/user/user.vue'
import Home from '@/components/user/hoem.vue'
import RoleList from '@/components/permission/rolelist.vue'
import Rights from '@/components/permission/rights.vue'
import GoodsList from '@/components/goodslist/goods.vue'
import AddEdit from '@/components/goodslist/add.vue'
import params from '@/components/goodslist/params.vue'
import categories from '@/components/goodslist/categories.vue'
import orderList from '@/components/order/orderList.vue'
import Reports from '@/components/reports/reports.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'logon',
    component: Logon
  },
  {
    path: '/list/',
    name: 'list',
    component: List,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'home',
        component: Home,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'users',
        name: 'users',
        component: User,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'roles',
        name: 'roles',
        component: RoleList,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'rights',
        name: 'rights',
        component: Rights,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'goods',
        name: 'goods',
        component: GoodsList,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'goods/add',
        name: 'goodsadd',
        component: AddEdit,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'goods/params',
        name: 'params',
        component: params,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'goods/categories',
        name: 'categories',
        component: categories,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'orders',
        name: 'orders',
        component: orderList,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      },
      {
        path: 'reports',
        name: 'reports',
        component: Reports,
        beforeEnter: (to, from, next) => {
          const token = localStorage.getItem('token')
          token ? next() : next('/')
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/list') {
    const token = localStorage.getItem('token')
    token ? next() : next('/')
  } else {
    next()
  }
})

export default router